package edu.sysu.cshare.gui;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import edu.sysu.cshare.control.UserManager;
import edu.sysu.cshare.model.User;
import java.awt.Color;

public class Dlg_Login extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txt_Id;
	private final JButton btn_Login = new JButton("登陆");
	private JPasswordField passwordField;
	private JButton btn_Quit;
	private JLabel lblcshare;
	private User actor;
	private JButton button;
	private JLabel lblWaiting;

	public User getUser() {
		return actor;
	}

	/**
	 * Launch the application.
	 * 
	 * public static void main(String[] args) { try { Dlg_Login dialog = new
	 * Dlg_Login(null);
	 * dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	 * dialog.setVisible(true); } catch (Exception e) { e.printStackTrace(); } }
	 */
	/**
	 * Create the dialog.
	 */
	public Dlg_Login() {
		setBackground(new Color(0, 0, 255));
		setBounds(100, 100, 322, 219);
		setLocationRelativeTo(null);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBackground(new Color(224, 255, 255));
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		GridBagLayout gbl_contentPanel = new GridBagLayout();
		gbl_contentPanel.columnWidths = new int[] { 70, 0, 0, 0 };
		gbl_contentPanel.rowHeights = new int[] { 0, 0, 28, 0, 0, 0, 0 };
		gbl_contentPanel.columnWeights = new double[] { 0.0, 0.0, 0.0,
				Double.MIN_VALUE };
		gbl_contentPanel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, Double.MIN_VALUE };
		contentPanel.setLayout(gbl_contentPanel);
		{
			lblcshare = new JLabel("欢迎使用CShare");
			lblcshare.setFont(new Font("宋体", Font.PLAIN, 24));
			GridBagConstraints gbc_lblcshare = new GridBagConstraints();
			gbc_lblcshare.gridwidth = 2;
			gbc_lblcshare.fill = GridBagConstraints.HORIZONTAL;
			gbc_lblcshare.insets = new Insets(0, 0, 5, 0);
			gbc_lblcshare.gridx = 1;
			gbc_lblcshare.gridy = 1;
			contentPanel.add(lblcshare, gbc_lblcshare);
		}
		{
			lblWaiting = new JLabel("正在登录 请稍后...");
			lblWaiting.setVisible(false);
			GridBagConstraints gbc_lblWaiting = new GridBagConstraints();
			gbc_lblWaiting.gridwidth = 2;
			gbc_lblWaiting.insets = new Insets(0, 0, 0, 5);
			gbc_lblWaiting.gridx = 1;
			gbc_lblWaiting.gridy = 5;
			contentPanel.add(lblWaiting, gbc_lblWaiting);
		}
		{
			JLabel lbl_ID = new JLabel("学号：");
			GridBagConstraints gbc_lbl_ID = new GridBagConstraints();
			gbc_lbl_ID.insets = new Insets(0, 0, 5, 5);
			gbc_lbl_ID.gridx = 0;
			gbc_lbl_ID.gridy = 2;
			contentPanel.add(lbl_ID, gbc_lbl_ID);
		}
		{
			txt_Id = new JTextField();
			txt_Id.setText("1221");
			GridBagConstraints gbc_txt_Id = new GridBagConstraints();
			gbc_txt_Id.gridwidth = 2;
			gbc_txt_Id.insets = new Insets(0, 0, 5, 0);
			gbc_txt_Id.fill = GridBagConstraints.HORIZONTAL;
			gbc_txt_Id.gridx = 1;
			gbc_txt_Id.gridy = 2;
			contentPanel.add(txt_Id, gbc_txt_Id);
			txt_Id.setColumns(10);
		}
		{
			JLabel lbl_psw = new JLabel("密码：");
			GridBagConstraints gbc_lbl_psw = new GridBagConstraints();
			gbc_lbl_psw.insets = new Insets(0, 0, 5, 5);
			gbc_lbl_psw.gridx = 0;
			gbc_lbl_psw.gridy = 3;
			contentPanel.add(lbl_psw, gbc_lbl_psw);
		}
		{
			passwordField = new JPasswordField();
			GridBagConstraints gbc_passwordField = new GridBagConstraints();
			gbc_passwordField.gridwidth = 2;
			gbc_passwordField.insets = new Insets(0, 0, 5, 0);
			gbc_passwordField.fill = GridBagConstraints.HORIZONTAL;
			gbc_passwordField.gridx = 1;
			gbc_passwordField.gridy = 3;
			contentPanel.add(passwordField, gbc_passwordField);
		}
		GridBagConstraints gbc_btn_Login = new GridBagConstraints();
		gbc_btn_Login.anchor = GridBagConstraints.EAST;
		gbc_btn_Login.insets = new Insets(0, 0, 5, 5);
		gbc_btn_Login.gridx = 1;
		gbc_btn_Login.gridy = 4;
		btn_Login.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// TODO how to make it show as soon as i click the button?????
				lblWaiting.setVisible(true);
				Runnable t = new Runnable() {

					@Override
					public void run() {
						try {
							// TODO:还原之前的的登录代码
							/*Dlg_Login.this.actor = UserManager
									.getUserById(txt_Id.getText());
							*/
							Dlg_Login.this.actor = UserManager.login(
									txt_Id.getText(),
									String.valueOf(passwordField.getPassword()));

							Dlg_Login.this.dispose();

						} catch (Exception e1) {
							JOptionPane.showConfirmDialog(null,
									e1.getMessage(), "Login Failure",
									JOptionPane.OK_OPTION);
						}
					}
				};
				t.run();

			}
		});
/*		{
			button = new JButton("作弊模式");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						Dlg_Login.this.actor = UserManager.login("admin",
								"admin");
					} catch (Exception e1) {
						e1.printStackTrace();
					}
					Dlg_Login.this.dispose();
				}
			});
			GridBagConstraints gbc_button = new GridBagConstraints();
			gbc_button.insets = new Insets(0, 0, 5, 5);
			gbc_button.gridx = 0;
			gbc_button.gridy = 4;
			contentPanel.add(button, gbc_button);
	}
*/		
		contentPanel.add(btn_Login, gbc_btn_Login);
		{
			btn_Quit = new JButton("退出");
			btn_Quit.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.exit(1);
				}
			});
			GridBagConstraints gbc_btn_Quit = new GridBagConstraints();
			gbc_btn_Quit.insets = new Insets(0, 0, 5, 0);
			gbc_btn_Quit.anchor = GridBagConstraints.WEST;
			gbc_btn_Quit.gridx = 2;
			gbc_btn_Quit.gridy = 4;
			contentPanel.add(btn_Quit, gbc_btn_Quit);
		}
		this.getRootPane().setDefaultButton(btn_Login);

	}

}
